var url;
$(document).ready(function(){
	validateModifyForm();
	validateAddForm();
})


/**
 * 新增仓库
 */

$(".repoadd").on("click", function(e){
	e.preventDefault();
	$("#addRepo").modal("show");
})

/**
 * 仓库编号自动生成
 */
$(".autoCode").click(function(){
	var code = "CK" + parseInt(Math.random() * (999999 - 100000 + 1) + 999999);
	$("#a_repoCode").val(code);
})


function validateAddForm() {

	$("#repoAddForm").validate({
		onsubmit:true,// 是否在提交是验证
	　　	onfocusout:false,// 是否在失去焦点时验证
	　　	onkeyup :false,// 是否在敲击键盘时验证
		rules:{
			repoCode:{
				required: true,
				codeFormat: true
			}
			,
			repoName: {
				required: true,
				maxlength:5
			},
			manager:{
				required: true,
				maxlength:10
			},
			maxSpace: {
				required: true,
				range:[1, 1000]
			},
		},
		messages: {
			repoCode:{
				required: "仓库编号必填",
			},
			repoName: {
				required: "仓库名称必填",
				maxlength:"仓库名称长度不能超过5"
			},
			manager:{
				required: "仓库经理必填",
				maxlength:"姓名长度不能超过10"
			},
			maxSpace: {
				required: "仓库的最大存储空间必填,请合理分配",
				range:"仓库存储空间必须在1-1000之间"
			},
		},
		
		errorPlacement: function(error, element){
			$("#add-errtip").show();
			$("#add-errtip").removeClass("alert-info").addClass("alert-danger");
			$("#add-errtip").html(error);
		},
		submitHandler: function(form){
			form.submit();
		},
		invalidHandler: function(form, validator) {
			return false;}
		
	})
	
	 jQuery.validator.addMethod("codeFormat",function(value,element){
		 var reg = /^CK.[a-zA-Z0-9]{6,}$/;
         return this.optional(element) || reg.test(value);
      }, "仓库编号必须以大写的CK开头且长度不能少于8位");
}



function validateModifyForm() {

	$("#repoForm").validate({
		onsubmit:true,// 是否在提交是验证
	　　	onfocusout:false,// 是否在失去焦点时验证
	　　	onkeyup :false,// 是否在敲击键盘时验证
		rules:{
			repoName: {
				required: true,
			},
			manager:{
				required: true
			},
			maxSpace: {
				required: true,
				range:[1, 1000]
			},
		},
		messages: {
			repoName: {
				required: "仓库名称必填",
			},
			manager:{
				required: "仓库经理必填"
			},
			maxSpace: {
				required: "仓库的最大存储空间必填,请合理分配",
				range:"仓库存储空间必须在1-1000之间"
			},
		},
		
		errorPlacement: function(error, element){
			$("#modify-errtip").show();
			$("#modify-errtip").removeClass("alert-info").addClass("alert-danger");
			$("#modify-errtip").html(error);
		},
		submitHandler: function(form){
			form.submit();
		},
		invalidHandler: function(form, validator) {
			return false;}
		
	})
}

/**
 * 物资修改页面
 */
$(".repoedit").on("click", function(e){
	url = $(this).attr("url");
	var obj = $(this);
	var repoCode = obj.attr("repo-code");
	$.get(url + "basic/repo/modify", {"repoCode": repoCode}, function(result){
		console.log(result);
		var data = result.msg;
		//var data = JSON.parse(result.msg);
		if (data != null && data != "fail") {
			//将值赋给模态框
			$("#m_repoCode").val(data.repoCode);
			$("#m_repoName").val(data.repoName);
			$("#m_manager").val(data.manager);
			$("#m_leftSpace").val(data.leftSpace == null ? data.maxSpace : data.leftSpace);
			$("#m_maxSpace").val(data.maxSpace);
		} else {
			alert("无法获取仓库"+ repoCode +"信息,进行修改!");
		}
	}, "json");
	
	
	//打开模态框
	e.preventDefault();
	$('#modifyRepo').modal('show');
	
})


/**
 * 清仓
 */

$(".repoclear").on("click", function(e){
	var repoCode = $(this).attr("repo-code");
	url =  $(this).attr("url");
	
	$("#c_repoCode").val(repoCode);
	$("#clearInfo").html("确定要清理仓库编号为【 " + repoCode +"】的下的所有物资信息吗 ?");
	
	e.preventDefault();
	$('#clearRepo').modal('show');
})


/**
 * 执行清仓
 */
$("#clearBtn").click(function(){
	var repoCode = $("#c_repoCode").val();
	var obj = $(".cleartip");
	var tip;
	
	$.get(url + "basic/repo/clear", {"repoCode": repoCode}, function(result){
		consoel.log("清仓结果:" + result)
		if (result.msg == "success") {
			tip = "{\"text\":\"清理完成\", \"layout\":\"topRight\",\"type\":\"success\"}";
			obj.attr("data-noty-options", tip);
			obj.trigger("click");
			$(window).attr('location', url + "base/repolist.html");
		} else if(result.msg == "fail"){
			tip = "{\"text\":\"不存在该仓库,清理失败\", \"layout\":\"center\",\"type\":\"error\"}";
		} else {
			tip = "{\"text\":\""+ result.error +"\", \"layout\":\"center\",\"type\":\"error\"}";
		}
		obj.attr("data-noty-options", tip);
		obj.trigger("click");
	}, "json");
	
})





/**
 * 删除模态框的显示
 */
$(".repodel").on("click", function(e){
	
	var repoCode = $(this).attr("repo-code");
	url =  $(this).attr("url");
	
	$("#d_repoCode").val(repoCode);
	$("#delInfo").html("您正在删除仓库编号为【 " + repoCode +"】的记录!<br>该操作不可恢复, 是否继续?");
	
	e.preventDefault();
	$('#delRepo').modal('show');
})


/**
 * 执行删除
 */
$("#delBtn").click(function(){
	var repoCode = $("#d_repoCode").val();
	var obj = $(".deltip");
	var tip;
	
	$.get(url + "basic/repo/del", {"repoCode": repoCode}, function(result){
		if (result.msg == "success") {
			tip = "{\"text\":\"成功移除一个仓库\", \"layout\":\"topLeft\",\"type\":\"success\"}";
			obj.attr("data-noty-options", tip);
			$(".deltip").trigger("click");
			$(window).attr('location', url + "base/repolist.html");
		} else if(result.msg == "fail"){
			tip = "{\"text\":\"不存在该仓库,删除失败\", \"layout\":\"center\",\"type\":\"error\"}";
		} else {
			tip = "{\"text\":\""+ result.error +"\", \"layout\":\"center\",\"type\":\"error\"}";
		}
		obj.attr("data-noty-options", tip);
		$(".deltip").trigger("click");
	}, "json");
	
})


